"""
@author: Sergiy Tsymbalyuk
"""

from sqlalchemy import Column, ForeignKey, Table, and_
from sqlalchemy.types import Integer, String, Text, Date, Boolean
from sqlalchemy.orm import relation

from pylons.i18n import lazy_ugettext as _
from babel import dates

from chowa.model.organization import Institution, District, Division
from chowa.model.abstract import person_full_name
from hrms.model.meta import Base

__all__ = ['Order', 'OrderType',
           'Position',
           'Appointment', 'Dismissal', 'Ranking',
           'AwardType', 'Award']


class OrderType(Base):
    __tablename__ = 'hrms_order_type'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100), info={'verbose': _('Object|Name')})
    description = Column(Text, info={'verbose': _('Description')})
    
    def __unicode__(self):
        return self.name
    
    def __repr__(self):
        return self.name


class Order(Base):
    __tablename__ = 'hrms_order'
    
    id = Column(Integer, primary_key=True)
    number = Column(String(10), info={'verbose': _('Number')})
    approved = Column(Date, info={'verbose': _('Approved')})
    type_id = Column(Integer, ForeignKey(OrderType.id), info={'verbose': _('Type')})
    institution_id = Column(Integer, ForeignKey(Institution.id))
    
    type = relation(OrderType)
    institution = relation(Institution)
    
    def __unicode__(self):
        short_date = dates.format_date(self.approved, format='short')
        return _('Order #%s since %s') % (self.number, short_date)

class Category(Base):
    __tablename__ = 'hrms_category'
    
    id = Column(Integer, primary_key=True)
    description = Column(Text)
    

class Position(Base):
    __tablename__ = 'hrms_position'
    __mapper_args__ = {'order_by': 'name'}
    
    id = Column(Integer, primary_key=True)
    name = Column(String(255), unique=True, info={'verbose': _('Object|Name')})
    category_id = Column(Integer, ForeignKey(Category.id))
    
    def __unicode__(self):
        return self.name


from hrms.model.person import Employee

class Appointment(Base):
    __tablename__ = 'hrms_appointment'
    
    id = Column(Integer, primary_key=True)
    position_id = Column(Integer, ForeignKey(Position.id), info={'verbose': _('Position')})
    division_id = Column(Integer, ForeignKey(Division.id), info={'verbose': _('Division')})
    employee_id = Column(Integer, ForeignKey(Employee.id), info={'verbose': _('Employee')})
    order_id = Column(Integer, ForeignKey(Order.id), info={'verbose': _('Order')})
    executed = Column(Date, info={'verbose': _('Executed')})
    is_acting = Column(Boolean, default=True, info={'verbose': _('Acting')})
    
    position = relation(Position, order_by=Position.name)
    division = relation(Division, order_by=Division.name)
    employee = relation(Employee, backref='appointments', order_by=person_full_name)
    order = relation(Order, order_by=Order.approved.desc(), backref='appointments')
    
    def __unicode__(self):
        return '%s (%s)' % (unicode(self.employee), unicode(self.position))
    

class Dismissal(Base):
    __tablename__ = 'hrms_dismissal'
    
    id = Column(Integer, primary_key=True)
    position_id = Column(Integer, ForeignKey(Position.id), info={'verbose': _('Position')})
    division_id = Column(Integer, ForeignKey(Division.id), info={'verbose': _('Division')})
    employee_id = Column(Integer, ForeignKey(Employee.id), info={'verbose': _('Employee')})
    order_id = Column(Integer, ForeignKey(Order.id), info={'verbose': _('Order')})
    executed = Column(Date, info={'verbose': _('Executed')})
    
    position = relation(Position, order_by=Position.name)
    division = relation(Division, order_by=Division.name)
    employee = relation(Employee, backref='dismissals', order_by=person_full_name)
    order = relation(Order, backref='dismissals',
                     order_by=Order.approved.desc())
    
    def __unicode__(self):
        return unicode(self.employee)


class Ranking(Base):
    __tablename__ = 'hrms_ranking'
    
    id = Column(Integer, primary_key=True)
    rank = Column(Integer, info={'verbose': _('Rank')})
    employee_id = Column(Integer, ForeignKey(Employee.id))
    order_id = Column(Integer, ForeignKey(Order.id), info={'verbose': _('Order')})
    executed = Column(Date, info={'verbose': _('Executed')})
    
    employee = relation(Employee)
    order = relation(Order, order_by=Order.approved.desc(), backref='rankings')
    
    def __unicode__(self):
        return '%s (%d)' % (unicode(self.employee), self.rank)


class AwardType(Base):
    __tablename__ = 'hrms_award_type'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100), info={'verbose': _('Object|Name')})
    
    def __unicode__(self):
        return self.name


class Award(Base):
    __tablename__ = 'hrms_award'
    
    id = Column(Integer, primary_key=True)
    type_id = Column(Integer, ForeignKey(AwardType.id), info={'verbose': _('Type')})
    employee_id = Column(Integer, ForeignKey(Employee.id), info={'verbose': _('Employee')})
    order_id = Column(Integer, ForeignKey(Order.id), info={'verbose': _('Order')})
    institution_id = Column(Integer, ForeignKey(Institution.id), info={'verbose': _('Institution')})
    district_id = Column(Integer, ForeignKey(District.id), info={'verbose': _('District')})
    
    type = relation(AwardType)
    employee = relation(Employee, backref='awards')
    order = relation(Order, order_by=Order.approved.desc(), backref='awards')
    institution = relation(Institution, order_by=Institution.short_name)
    district = relation(District, order_by=District.treasury_code)
    
    def __unicode__(self):
        return unicode(self.type)
